package cn.sdormitory.service.impl;

import cn.sdormitory.basedata.dao.BClassDao;
import cn.sdormitory.basedata.dao.BStudentDao;
import cn.sdormitory.basedata.entity.BStudent;
import cn.sdormitory.basedata.vo.BClassVo;
import cn.sdormitory.common.utils.DateTimeUtils;
import cn.sdormitory.morningExercises.dao.MorningExercisesDao;
import cn.sdormitory.morningExercises.entity.MorningExercises;
import cn.sdormitory.morningExercises.vo.AppMorningExercisesCountVO;
import cn.sdormitory.morningExercises.vo.AppMorningExercisesToStuVO;
import cn.sdormitory.retention.AppRetentionCountVO;
import cn.sdormitory.retention.DormitoryRetention;
import cn.sdormitory.service.AppMorningExercisesService;
import cn.sdormitory.sys.dao.SysDeptDao;
import cn.sdormitory.sys.dao.SysUserDao;
import cn.sdormitory.sys.entity.SysUser;
import cn.sdormitory.sys.service.SysUserRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author : yy
 * @date: 2024/9/20 11:28
 * @description:
 */
@Service
public class AppMorningExercisesServiceImpl implements AppMorningExercisesService {
    @Resource
    private MorningExercisesDao exercisesDao;

    @Resource
    private SysUserRoleService sysUserRoleService;
    @Resource
    private BClassDao classDao;
    @Resource
    private SysUserDao sysUserDao;
    @Resource
    private BStudentDao bStudentDao;
    @Resource
    private SysDeptDao sysDeptDao;

    @Override
    public List<AppMorningExercisesToStuVO> listTodayMorningExercises() {
        return exercisesDao.listTodayMorningExercises();
    }

    @Override
    public void exercisesRegister(Long studentId, Long classId, Integer type, String registrantUser) {
        LambdaQueryWrapper<MorningExercises> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MorningExercises::getStudentId,studentId);
        queryWrapper.apply("DATE(registrant_date) = CURDATE()");
        MorningExercises morningExercises = exercisesDao.selectOne(queryWrapper);

        if (!Optional.ofNullable(morningExercises).isPresent()) {
            MorningExercises exercises  = new MorningExercises();
            exercises.setStudentId(studentId);
            exercises.setClassId(classId);
            exercises.setType(type);
            exercises.setRegistrantUser(registrantUser);
            exercises.setRegistrantDate(new Date());
            exercisesDao.insert(exercises);
        }
    }

    @Override
    public void delMorningExercises(Long id) {
        exercisesDao.deleteById(id);
    }

    @Override
    public List<AppMorningExercisesCountVO> getAppMorningExercisesCount(Long userId, String rq) {
        boolean isDeptManager = false ; //是否部门经理
        List<Long> roleIdList = sysUserRoleService.listRoleIdByUserId(userId);
        for (Long roleId : roleIdList) {
            if(roleId == 2) {
                isDeptManager = true ;
                break;
            }
        }
        List<BClassVo> classList = new ArrayList<>();
        if(isDeptManager) {
            SysUser user = sysUserDao.selectById(userId);
            List<Long> deptIds = sysDeptDao.getAllChildByParent(user.getDeptId());
            //classList = classDao.getAppClassDeptByClaId(user.getDeptId());
            classList = classDao.getAppClassByDeptIds(deptIds);
        }else {
            List<BClassVo> cList = classDao.getAppClassListByTeaId(userId);
            for (BClassVo bClassVo : cList) {
                LambdaQueryWrapper<BStudent> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(BStudent::getClassId, bClassVo.getClassId())
                        .eq(BStudent::getIsGraduated, "0");
                List<BStudent> studentList = bStudentDao.selectList(wrapper);
                if (!studentList.isEmpty()) {
                    classList.add(bClassVo);
                }
            }
        }

        return classList.parallelStream()
                .map(classVo -> {
                    Long classId = classVo.getClassId();
                    AppMorningExercisesCountVO countVO = new AppMorningExercisesCountVO();
                    countVO.setClassName(classVo.getClassName());
                    countVO.setClassId(classId);

                    Long classNum = classDao.getStuCountByClaId(classId);
                    countVO.setClassNum(classNum);

                    Integer liveNum = classDao.getBoardingStuCountByClassId(classId.toString());
                    countVO.setLiveNum(liveNum);

                    LambdaQueryWrapper<MorningExercises> retentionWrapper = new LambdaQueryWrapper<>();
                    retentionWrapper.eq(MorningExercises::getClassId,classId);
                    retentionWrapper.eq(MorningExercises::getRegistrantDate, DateTimeUtils.strToDate(rq));
                    Integer count = exercisesDao.selectCount(retentionWrapper);
                    countVO.setExercisesNum(count);

                    return countVO;
                })
                .collect(Collectors.toList());
    }
    
    @Override
    public List<MorningExercises> getMonthlyStatisticsMorningExercises(List<Long> classIds, String accessDate) {
        return exercisesDao.getMonthlyMorningExercises(classIds, accessDate);
    }
    
    @Override
    public List<AppMorningExercisesToStuVO> getMorningExercisesStuByClassId(Long classId, String rq) {
        return exercisesDao.getMorningExercisesStuByClassId(classId,rq);
    }
}
